/* $RCSFile: FAQsEdit.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/FAQs/FAQsEdit.aspx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:20  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:54  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Globalization;

using AddonNice.Helpers;
using AddonNice.Security;
using AddonNice.UI;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.UI.WebControls;
using AddonNice.Settings.PageModules.FAQs;

using Trace2        =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.FAQs
{

	/// <summary>
	/// IBS Portal FAQ module - Edit page part
	/// </summary>
	public partial class FAQsEdit : AddonNice.UI.EditItemPage
	{
		protected IHtmlEditor DesktopText   =   null;
		FAQsDB questions			        =   new FAQsDB();


        public FAQsEdit()
            : base(true, true, true, true)
        { }

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FAQsEdit Page_Load  BEG IsPostBack: {0}, IsInAsyncPostBack: {1}.",IsPostBack,IsInAsyncPostBack),PageTraceSwitch.Sw.Info);
			DesktopText					        = GetEditor(PlaceHolderHTMLEditor);

			//	populate with FAQ Details  
			if (!IsPostBack) 
			{

				if ( !isNewItem  ) 
				{
					//  get a single row of FAQ info
					SqlDataReader dr			    =   null;
					try
					{
						dr						    =	questions.GetSingleFAQsGlobalized(ItemID);
						//  Read database
						if ( dr.Read() )
						{
							Question.Text		    =   (string) dr["Question"];
							DesktopText.TextContent	=   (string) dr["Answer"];
							CreatedBy.Text		    =   (string) dr["CreatedByUser"];
							CreatedDate.Text	    =   ((DateTime) dr["CreatedDate"]).ToShortDateString();
							if (CreatedBy.Text == StStr.unknown || CreatedBy.Text == string.Empty )
							{
								CreatedBy.Text      =   LocalizeHelper.Localize( StStr.UNKNOWN, StStr.unknown);
							}
						}
					}
					catch(Exception ex)
					{
						if (PageTraceSwitch.Sw.TraceError)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FAQsEdit Page_Load  ex : {0}",ex),PageTraceSwitch.Sw.Info);
                        throw;
					}
					finally
					{
						if ( dr != null )
							dr.Close();
					}
				}
			}
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]FAQsEdit Page_Load END.",PageTraceSwitch.Sw.Info);
		}

		protected override ArrayList AllowedModules
		{
			get
			{
				ArrayList al = new ArrayList();
				al.Add ("2502DB18-B580-4F90-8CB4-C15E6E531000");
				return al;
			}
		}

        void FAQsEdit_Update(object sender, ADNPageEventArgs e)
		{
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FAQsEdit FAQsEdit_Update Question.Text: {0}, DesktopText.Text: {1}",Question.Text,DesktopText.TextContent),PageTraceSwitch.Sw.Info);
			// Don't Allow empty data
			if ( Question.Text == string.Empty || DesktopText.TextContent == string.Empty) 
				return;

			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FAQsEdit FAQsEdit_Update Page.IsValid: {0}", Page.IsValid),PageTraceSwitch.Sw.Info);
			//  Update only if entered data is valid
			try
			{
				string Pseudo	=	PortalSecurity.Wrapper.userName;
				if (PageTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FAQsEdit FAQsEdit_Update isNewItem: {0}", isNewItem),PageTraceSwitch.Sw.Info);
				if ( isNewItem ) 
				{
					//  Add the question within the questions table
					questions.AddFAQs(ModuleID, Pseudo, Question.Text, DesktopText.TextContent);
				} 
				else 
				{
					//  Update the question within the questions table
					questions.UpdateFAQsGlobalized(ModuleID, ItemID, Pseudo, Question.Text, DesktopText.TextContent);
				}
			}
			catch(Exception ex)
			{
				if (PageTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FAQsEdit FAQsEdit_Update ex : {0}",ex),PageTraceSwitch.Sw.Info);
			}
		}

        void FAQsEdit_Delete(object sender, ADNPageEventArgs e)
		{
			//  Only attempt to delete the item if it is an existing item
			if ( !isNewItem  ) 
			{
				try
				{
					questions.DeleteFAQsGlobalized(ItemID);
				}
				catch(Exception ex)
				{
					if (PageTraceSwitch.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FAQsEdit FAQsEdit_Delete ex : {0}",ex),PageTraceSwitch.Sw.Info);
                    throw;
				}

			}
		}


		#region Web Form Designer generated code
        
        /// <summary>
        /// Here we create the command buttons and add them to the PlaceHolders prepared
        /// </summary>
        /// <param name="e"></param>
        override protected void OnPreInit(EventArgs e)
        {
			if ( PageTraceSwitch.Sw.TraceVerbose )
    			Trace2.WriteLine("[V]FAQEdit OnPreInit Begin");
            if ( isNewItem ) 
                PageHasDeleteBtn    =   false;
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FAQEdit OnPreInit PageHasDeleteBtn: {0}.",PageHasDeleteBtn),PageTraceSwitch.Sw.Info);
            base.OnPreInit(e);
			if ( PageTraceSwitch.Sw.TraceVerbose )
			    Trace2.WriteLine("[V]FAQEdit  OnPreInit End");
        }

        /// <summary>
		/// Raises OnInitEvent
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() 
		{    
			Load    +=  new EventHandler(Page_Load);
            Update  +=  new PageEventHandler(FAQsEdit_Update);
            Delete  +=  new PageEventHandler(FAQsEdit_Delete);
		}


        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons
        {
            get { return PlaceHolderButtons; }
            set { }
        }

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons2
        {
            get { return PlaceHolderButtons2; }
            set { }
        }
		#endregion

	
	}
}
